
type typeOptions<T> = [
  prop: keyof T,
  values: string | string[] | ((row: T) => boolean)
]
export type rowStatusType<T extends Record<string, any>> = Partial<
  Record<Colors, typeOptions<T>>
>


/**
 * 统一设置表格行禁用样式, 增加om-table-column--disable
 * @param prop column.prop
 * @param values column.prop对应显示disabled的值
 * @param type 根据传入type对应显示om-table-column--{type}的样式
 * @returns tableRowClassName
 */
export function useRowStatusDisable<T extends Record<string, any>>(
  prop: keyof T,
  values: string | string[] | ((row: T) => boolean) = '禁用',
  type: Colors = 'disabled'
) {
  return ({ row }: { row: T }) => {
    let isDisabled = false
    switch (typeof values) {
      case 'object': {
        if (Array.isArray(values)) {
          isDisabled = values.includes(row[prop])
        }
        break
      }
      case 'string': {
        isDisabled = row[prop] === values
        break
      }
      case 'function': {
        isDisabled = values(row)
        break
      }
      default: {
        break
      }
    }
    return isDisabled ? `gl-table-column--${type}` : ''
  }
}

/**
 * 统一设置表格行禁用、警告、错误等样式, 根据styleOptions参数增加om-table-column--{type}
 * @param styleOptions 设定禁用、警告、错误等样式配置
 * @returns tableRowClassName
 */
export function useRowStatusStyle<T extends Record<string, any>>(styleOptions: rowStatusType<T>) {
  return ({ row }: { row: T }) => {
    let rowClass = ''
    let key: keyof rowStatusType<T>
    for (key in styleOptions) {
      const prop = styleOptions[key]![0]
      const values = styleOptions[key]![1]
      rowClass = useRowStatusDisable(prop, values, key)({ row }) || rowClass
    }
    return rowClass
  }
}
